import util from '@/libs/util';

export default {
    name: 'exportable-table',
    data() {
        return {
            columns: [{
                title: '序号',
                key: 'rownum',
                width: 60,
                align: 'center',
                render: (h, params) => {
                    return h('div', {}, ((this.pageIndex - 1) * this.pageSize + params.index + 1))
                }
            }, {
                title: '角色名称',
                key: 'name'
            }, {
                title: '描述',
                key: 'description'
            }, {
                title: '状态',
                key: 'status',
                render: (h, params) => {
                    let texts = '';
                    if (params.row.status === 1) {
                        texts = "正在使用"
                    } else if (params.row.status === 2) {
                        texts = "停止使用"
                    }
                    return h('div', {}, texts);
                }
            }, {
                title: '操作',
                key: 'action',
                align: 'center',
                render: (h, params) => {
                    return h('div', [
                        h('Button', {
                            props: {
                                type: 'primary',
                                size: 'small'
                            },
                            style: {
                                marginRight: '5px'
                            },
                            on: {
                                click: () => {
                                    this.showEdit(params.row);
                                }
                            }
                        }, '修改'),
                        h('Button', {
                            props: {
                                type: 'error',
                                size: 'small'
                            },
                            on: {
                                click: () => {
                                    this.delete(params.row.id);
                                }
                            }
                        }, '删除')
                    ]);
                }
            }],
            data: [],
            formData: {},
            menuList: [],
            total: 0,
            pageIndex: 1,
            pageSize: 10,
            viewModalVisible: false,
            listStyle: {
                height: '300px'
            },
            titles: ['可添加模块', '已添加模块']
        };
    },
    mounted() {
        this.loadList();
    },
    methods: {
        loadList() {
            util.ajax.post('/base/role/list', util.toFormData({
                page: this.pageIndex,
                size: this.pageSize
            })).then(res => {
                this.data = res.data.data.list;
                this.total = res.data.data.total;
            });
        },
        showAdd() {
            this.formData = {
                name: '',
                status: 1
            };
            this.setMenuList(null);
        },
        showEdit(obj) {
            util.ajax.post('/base/role/detail', util.toFormData({
                id: obj.id
            })).then(res => {
                if (res.data.data) {
                    this.formData = res.data.data;
                    this.setMenuList(obj.id);
                } else {
                    this.$Modal.error({
                        title: '警告',
                        content: '出错了'
                    });
                }
            });
        },
        save () {
            let formData = {};
            for (let key in this.formData) {
                formData[key] = this.formData[key];
            }
            let menuList = this.$refs.menuList.getCheckedNodes();
            let ids = [];
            menuList.forEach((item) => {
                if (item.parentId && ids.indexOf(item.parentId) === -1) {
                    ids.push(item.parentId);
                }
                if (ids.indexOf(item.id)) {
                    ids.push(item.id);
                }
            });
            formData['checkMenu'] = ids;
            util.ajax.post('/base/role/save', util.toFormData(formData)).then(res => {
                if (res.data.message === 'SUCCESS') {
                    this.viewModalVisible = false;
                    this.$Message.success('保存成功！');
                    this.loadList();
                } else {
                    this.$Message.warning(res.data.message || '未知错误！');
                }
            });
        },
        delete(id) {
            this.$Modal.confirm({
                title: '提示',
                content: '<p>确定删除？</p>',
                onOk: () => {
                    util.ajax.post('/base/role/delete', util.toFormData({
                        id: id
                    })).then(res => {
                        if (res.data.message === 'SUCCESS') {
                            this.$Message.success('删除成功！');
                            this.loadList();
                        }
                    });
                },
                onCancel: () => {
                }
            });
        },
        setMenuList(id) {
            let formData = {};
            if (id) {
                formData = {
                    id: id
                }
            }
            util.ajax.post('/base/menu/roleToMenu', util.toFormData(formData)).then(res => {
                if (res.data.data) {
                    let allMenu = res.data.data.allMenu;
                    let checkMenu = res.data.data.checkMenu;
                    let chekcList = [];
                    checkMenu.forEach((item, index) => {
                        chekcList.push(item.id);
                    })
                    let menuList = [];
                    allMenu.forEach((item, index) => {
                        if (item.level === 1) {
                            menuList.push({
                                id: item.id,
                                title: item.title,
                                expand: true,
                                children: []
                            });
                        } else if (item.level === 2) {
                            menuList[menuList.length - 1].children.push({
                                id: item.id,
                                parentId: item.parentId,
                                title: item.title,
                                expand: true,
                                checked: chekcList.indexOf(item.id) > -1
                            });
                        }
                    });
                    this.menuList = menuList;
                    this.viewModalVisible = true;
                } else {
                    this.$Modal.error({
                        title: '警告',
                        content: '出错了'
                    });
                }
            });
        }
    }
};